package cn.raven.order.service.impl;

import cn.raven.order.domain.OrderPetAcquisition;
import cn.raven.order.domain.OrdetFailedCquisitionLog;
import cn.raven.order.dto.OrderConfirmationDto;
import cn.raven.order.mapper.OrderPetAcquisitionMapper;
import cn.raven.order.mapper.OrdetFailedCquisitionLogMapper;
import cn.raven.order.service.IOrderPetAcquisitionService;
import cn.raven.basic.service.impl.BaseServiceImpl;
import cn.raven.pet.domain.Pet;
import cn.raven.pet.domain.SearchMasterMsg;
import cn.raven.pet.mapper.PetMapper;
import cn.raven.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Raven
 * @since 2022-07-19
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private OrdetFailedCquisitionLogMapper ordetFailedCquisitionLogMapper;
    
    /** 
    * @Title: confirm
    * @Description: 确认订单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 21:01
    * @Parameters: [dto]
    * @Return void
      */
    @Override
    public void confirm(OrderConfirmationDto dto) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findOne(dto.getId());
        //设置价格
        orderPetAcquisition.setPrice(dto.getMoney());
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPaytype(dto.getPayType());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //根据业务需求我们这次是设置价格
        Pet pet = petMapper.findOne(orderPetAcquisition.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
        //修改寻主信息状态
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(orderPetAcquisition.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    /**
    * @Title: cancel
    * @Description: 取消订单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/20 20:05
    * @Parameters: [id]
    * @Return void
      */
    @Override
    public void cancel(Long id) {
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findOne(id);
        //设置订单状态-1
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        //删除刚生成的宠物信息
        petMapper.del(orderPetAcquisition.getPetId());
        //将寻主消息的shopid设为空，放到寻主池
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(orderPetAcquisition.getSearchMasterMsgId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        //保存取消订单日志
        OrdetFailedCquisitionLog log = new OrdetFailedCquisitionLog();
        log.setOrderPetId(orderPetAcquisition.getId());
        ordetFailedCquisitionLogMapper.add(log);
    }
}
